package com.ly.cloud.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ly.cloud.dto.CodeDetailDto;
import com.ly.cloud.entity.CodeDetailPo;
import com.ly.cloud.exception.CloudException;
import com.ly.cloud.exception.biz.BusinessException;
import com.ly.cloud.exception.mapper.DBException;
import com.ly.cloud.mapper.CodeCategoryMapper;
import com.ly.cloud.mapper.CodeClassificationMapper;
import com.ly.cloud.mapper.CodeDetailMapper;
import com.ly.cloud.service.DataDictService;
import com.ly.cloud.utils.BeanCopyUtils;
import com.ly.cloud.vo.CodeDeailFidVo;
import com.ly.cloud.vo.CodeDetailVo;
import com.ly.cloud.vo.CodeMenuVo;

@Service
public class DataDictServiceImpl implements DataDictService{



	@Autowired
	private CodeClassificationMapper codeClassificationMapper;
	@Autowired
	private CodeCategoryMapper codeCategoryMapper;
	@Autowired
	private CodeDetailMapper codeDetailMapper;
	/** 
	 * Description:  
	 * 查询所有代码分类
	 * @author LuHuaCong
	 * @date: 2018年12月6日
	 */
	@Override
	public List<CodeMenuVo> selectCodeList(Long id) {
		List<CodeMenuVo> list = null;
		//如果不传入代码分类id
		if(id == null ) {
			try {
				list = codeClassificationMapper.selectCodeList();
			} catch (Exception e) {
				e.printStackTrace();
				throw new DBException("查询代码分类失败了！", e);
			}
		}else {
			try {
				list = codeCategoryMapper.selectCodeList(id);
			} catch (Exception e) {
				e.printStackTrace();
				throw new DBException("查询代码类别失败了！", e);
			}
		}				
		return list;
	}

	
	/**(non-Javadoc)
	 * Description: 
	 * 根据代码类别id查询代码详情列表
	 * Created On: 2018年12月11日
	 * @author LuHuaCong
	 * <p>
	 * @param page 
	 * @param id 代码分类id
	 * @return  Page<SystemSetVo>
	 */
	@Override
	public Page<CodeDetailVo> selectDetailList(Page<CodeDetailVo> page, String id,String condition) {
		try {
			return page.setRecords(codeDetailMapper.selectDetailListById(page, id,condition));
		} catch (Exception e) {
			throw new CloudException("查询异常，请重试!", e);
		}
	}

	/**
	 * Description: 修改代码详情
	 * @param dto	封装对象
	 * @return   是否修改成功   
	 * @author LuHuaCong
	 * @date: 2018年12月12日
	 */
	@Override
	public boolean updateCodeDetail(CodeDetailDto dto) {
		try {
			if(StringUtils.isBlank(dto.getDmlb())) {
				throw new BusinessException();
			}
			
			//由于fid数据库类型为number，可为空，所以通过自定义SQL更新 @caihuihai 2019-01-03
			Integer i = codeDetailMapper.updateCodeDetail(dto);
			if(i>0) {
				return true;
			}else {
				return false;
			}
		} catch (BusinessException e) {
			throw new BusinessException("dmlb代码类别需要传值", e);
		} catch (Exception e) {
			throw new BusinessException("更新代码详情失败！", e);
		}
	}

	/** 
	 * Method Name: insert
	 * Description:  
	 * 插入代码详情记录
	 * @param dto 
	 * @author LuHuaCong
	 * @date: 2018年12月12日
	 */
	@Override
	public boolean insert(CodeDetailDto dto,String loginUserName) {
		CodeDetailPo po = new CodeDetailPo();
		BeanUtils.copyProperties(dto, po);
		po.setCjsj(new Date());
		po.setXgr(loginUserName);
		try {
			Integer i = codeDetailMapper.insert(po);
			if(i>0) {
				return true;
			}else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new DBException("添加代码详情失败！", e);
			}
	}

	/** 
	 * Method Name: deleteDetailById
	 * Description:  
	 * 根据id删除代码详情
	 * @param id 
	 * @author LuHuaCong
	 * @date: 2018年12月12日
	 */
	@Override
	public boolean deleteDetailById(Long id) {
		try {
			Integer i = codeDetailMapper.deleteById(id);
			if(i>0) {
				return true;
			}else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new DBException("删除代码详情失败！", e);
		}
	}


	/** 
	 * Method Name: deleteDetailByIds
	 * Description:  
	 * 批量删除代码详情
	 * @param ids
	 * @return 删除条数
	 * @author LuHuaCong
	 * @date: 2018年12月12日
	 */
	@Override
	public Integer deleteDetailByIds(List<Long> ids) {
		try {
			return codeDetailMapper.deleteBatchIds(ids);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CloudException("批量删除代码详情失败!", e);
		}
	}

	/**
	 * 根据代码类别码（固定）查找对应的分类详情；
	 */
	@Override
	public List<CodeDetailVo> findDmxqByDmlb(String dmlb) {
		try {
			EntityWrapper<CodeDetailPo> ew = new EntityWrapper<>();
			ew.where("dmlb={0}", dmlb).orderBy("dm", true);  
			List<CodeDetailPo> poList = codeDetailMapper.selectList(ew);
			return BeanCopyUtils.copyListProperties(poList, CodeDetailVo.class);
		} catch (Exception e) {
			throw new CloudException("查询代码详情失败!", e);
		}
	}


	/** 
	 * Method Name: selectFid
	 * Description:  
	 * 查询fid,下拉列表
	 * 当dmlb为空时，查询所有的fid;当dmlb不为空时，查询该代码类别的fid
	 * param dmlb
	 * @return 父id列表
	 * @author LuHuaCong
	 * @date: 2018年12月20日
	 */
	@Override
	public List<CodeDeailFidVo> selectFid(String dmlb) {
		
		try {
			return codeDetailMapper.selectFid(dmlb);
		} catch (Exception e) {
			throw new CloudException("查询父id失败!", e);
		}
	}


	/** 
	 * Method Name: selectSourcePlace
	 * Description:  
	 * 选择生源地下拉按钮
	 * @return 生源地列表
	 * @author LuHuaCong
	 * @date: 2018年12月20日
	 */
	@Override
	public List<CodeDetailVo> selectSourcePlace() {
		/**
		 * 省份行政区码，前面两位__代表不同省份
		 */
		String rules = "__0000";
		EntityWrapper<CodeDetailPo> ew = new EntityWrapper<>();
		ew.where("DM like {0}", rules);
		List<CodeDetailVo> voList = null;
		try {
			List<CodeDetailPo> poList = codeDetailMapper.selectList(ew);
			voList = BeanCopyUtils.copyListProperties(poList, CodeDetailVo.class);
		} catch (Exception e) {
			throw new CloudException("查询生源地失败!", e);
		}
		
		return voList;
	}


	/**
	 * 查询某个代码分类下的所有代码详情
	 * @param dmfl 代码分类编号
	 * @return
	 * @date  2019/1/9
	 */
	@Override
	public Page<CodeDetailVo> selectDmxqByDmfl(Page<CodeDetailVo> page, String dmfl) {
		List<CodeDetailVo> voList = null;
		try {
			voList = codeDetailMapper.selectDmxqByDmfl(page, dmfl);
			return page.setRecords(voList);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CloudException("查询该分类下的代码详情失败",e);
		}
		
	}

}
